package br.com.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;

public class HibernateDAO<T> implements InterfaceDAO<T> {

	
	private Class<T> classe;
	private Session session;
	
	public HibernateDAO(Class<T> classe, Session session) {
		super();
		this.classe = classe;
		this.session = session;
	}

	@Override
	public void salvar(T bean) {
		session.save(bean);
		
	}

	@Override
	public void atualizar(T bean) {
		session.update(bean);
		
	}

	@Override
	public void excluir(T bean) {
		session.delete(bean);
		
	}

	public List<T> getBeans() {
		Criteria crit = session.createCriteria(classe);
		crit.setCacheable(true);

		List<T> beans = (List<T>)crit.list();
		return beans;
	}

	@Override
	public T getBean(Serializable codigo) {
		T bean = (T)session.get(classe, codigo);
		return bean;
	}

}
